#include <QResizeEvent>
#include <QPainter>
#include <QVariantList>

#include "appmanager.h"
#include "mytreeview.h"
#include "my_defs.h"

MyTreeView::MyTreeView(QWidget *parent) :
    QTreeView(parent)
{
  setUniformRowHeights(false);
}


void MyTreeView::changeEvent(QEvent *e)
{
    Q_UNUSED(e);

}

void MyTreeView::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
//    for (int i = 0; i<COL_COUNT; i++)
  //        resizeColumnToContents(i);
}

void MyTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &options, const QModelIndex &index) const
{
  QTreeView::drawRow( painter, options, index );
  if (index.parent()==QModelIndex()) // if parent style
    {
      QRect rect = options.rect;
      rect.setX(rect.x()+2);
      rect.setY(rect.y()+2);
      rect.setHeight(rect.height()-2);
      rect.setWidth(rect.width()-2);

//      painter->drawLine(rect.bottomLeft(), rect.bottomRight());
      painter->drawRoundedRect(rect, 10, 10);

      if (appManager->stylesLoaded() == AllStylesLoaded)
        {
          QModelIndex mi = model()->index(index.row(), 2, QModelIndex());
          int styleIndx = this->model()->data(mi).toInt();
          Style stl = appManager->mStyles.value(styleIndx);
//          qDebug()<<stl.img<<stl.name<<stl.uniqId;
          QPixmap pic(stl.img);
          pic = pic.scaledToHeight(PARENT_HEIGHT - 8, Qt::SmoothTransformation);
          painter->drawPixmap(rect.topLeft().x() + indentation(), rect.topLeft().y() + 2, pic);
//          painter->drawRect(QRect(rect.topLeft().x() + indentation(),
//                                  rect.topLeft().y() + 2,
//                                  rect.topLeft().x() + indentation() + pic.width(),
//                                  rect.topLeft().y() + 2 + pic.height()));
          painter->save();

          painter->setFont(QFont("Helvetica [Cronyx]", 16));
          QRect textRect(QPoint(rect.topLeft().x() + indentation() + pic.width() + indentation(),
                                rect.topLeft().y()),
                         QPoint(rect.topRight().x(),
                                rect.bottomRight().y()));
          painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, stl.name);

          painter->restore();
        }

  }
}
